/**
 * 
 */
package com.idea.auction.service;

import java.util.HashMap;
import java.util.Map;

import com.idea.auction.model.Admin;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.server.BaseService;

/**
 * @author y.z
 */

public class AdminService extends BaseService<Admin>{
	
	
	
	public Admin login(String account,String pwd){
		return super.findFirst("select * from sys_admin where account = '"+account+"' and password = '"+pwd+"'");
	}
	
	
	public Admin getByUserAccount(String account){
		return super.findFirst("select * from sys_admin where account = '"+account+"'");
	}
	
	/**
	 * 获取分页
	 * @param curPage
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Page<Admin> getPage(int curPage){
		Map<String, Object> indexMap = CacheKit.get("AdminListCach", curPage);
		
		if(null == indexMap){
			System.out.println("缓存不存在");
			indexMap = new HashMap<String, Object>();
			indexMap.put("adminPage",super.getPage(curPage, "select * ", "from sys_admin"));
			
			
			CacheKit.put("AdminListCach", curPage, indexMap);
		}
		
		
		return (Page<Admin>) indexMap.get("adminPage");
	}
	
	/**
	 * 删除管理员
	 * @param id
	 * @return
	 */
	public boolean delete(String id){
		return super.delById(id);
	}
	
	/**
	 * 添加管理员
	 * @param admin
	 * @return
	 */
	public boolean add(Admin admin){
		return super.save(admin);
	}
	
	/**
	 * 修改管理员
	 * @param admin
	 * @return
	 */
	public boolean update(Admin admin){
		return super.update(admin);
	}
	
	/**
	 * 根据id获取详细
	 * @param id
	 * @return
	 */
	public Admin getAdminById(String id){
		return super.findById(id);
	}
	
	
	/**
	 * 验证用户帐号
	 * @param id
	 * @param account
	 * @return
	 */
	public boolean validateAccount(String id,String account){
		String sql = "select * from sys_admin where id != '"+id+"' and account = '"+account+"'";
		
		if(StrKit.isBlank(id)){
			sql = "select * from sys_admin where account = '"+account+"'";
		}
		
		if(null == super.findFirst(sql)){
			return true;
		}
		return false;
	}
	
	public static void main(String[] args) {
		System.out.println(System.getProperty("java.io.tmpdir"));
	}
}
